package lol.clann.minecraft.springboot.api.model;


import lol.clann.minecraft.springboot.api.bukkit.constant.NBTTypeEnum;

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 便捷的操作lore
 *
 * @author pyz
 * @date 2019/5/21 10:23 PM
 */
public class Lores implements Iterable<String> {

    private List<Object> list;

    public Lores() {
        this.list = new ArrayList<>();
    }

    public Lores(List<Object> list) {
        this.list = list;
    }

    public boolean contains(String lore) {
        for (Object o : list) {
            String line = (String) NBTTypeEnum.NBTTagString.getField().get(o);
            if (Objects.equals(line, lore)) {
                return true;
            }
        }
        return false;
    }

    public boolean contains(Pattern lorePattern) {
        for (Object o : list) {
            String line = (String) NBTTypeEnum.NBTTagString.getField().get(o);
            if (lorePattern.matcher(line).matches()) {
                return true;
            }
        }
        return false;
    }

    public List<Object> getHandle() {
        return list;
    }

    public boolean remove(String lore) {
        Iterator<Object> it = list.iterator();
        boolean removed = false;
        while (it.hasNext()) {
            Object next = it.next();
            if (Objects.equals(unwrap(next), lore)) {
                it.remove();
                removed = true;
            }
        }
        return removed;
    }

    public boolean isEmpty() {
        return list.isEmpty();
    }

    public int size() {
        return list.size();
    }

    public void add(String lore) {
        list.add(wrap(lore));
    }

    public void addAll(Collection<String> lores) {
        if (lores != null) {
            for (String lore : lores) {
                add(lore);
            }
        }
    }

    public void set(int index, String lore) {
        Object tag = NBTTypeEnum.NBTTagString.getConstructor().invoke(null);
        NBTTypeEnum.NBTTagString.getField().set(tag, lore);
        if (index < 0) {
            index = 0;
        }
        if (index >= list.size()) {
            list.add(lore);
        } else {
            list.set(index, wrap(lore));
        }
    }

    public String remove(int index) {
        if (index < 0) {
            index = 0;
        }
        if (index >= list.size()) {
            return null;
        }
        return unwrap(list.remove(index));
    }

    public void clear() {
        list.clear();
    }

    public List<String> toStringList() {
        return list.stream()
                .map(this::unwrap)
                .collect(Collectors.toList());
    }

    @Override
    public Iterator<String> iterator() {
        return list.stream()
                .map(this::unwrap)
                .iterator();
    }

    private Object wrap(String str) {
        Object tag = NBTTypeEnum.NBTTagString.getConstructor().invoke(null);
        NBTTypeEnum.NBTTagString.getField().set(tag, str);
        return tag;
    }

    private String unwrap(Object tag) {
        return (String) NBTTypeEnum.NBTTagString.getField().get(tag);
    }

}
